//
//  LoginVC.m
//  ShanMei
//
//  Created by huazi on 15/3/9.
//  Copyright (c) 2015年 huazi. All rights reserved.
//

#import "LoginVC.h"
#import "AppDelegate.h"
#define kbtnRemberAccount  @"#ed1f2c"
@interface LoginVC ()<UITextFieldDelegate>
@property (strong, nonatomic)UIView *viewHeadBJ;
@property (strong, nonatomic)UIView *viewCenter;
@property (strong, nonatomic)UITextField *textAccount;
@property (strong, nonatomic)UITextField *textPassword;
@property (strong, nonatomic)UIButton *btnLogin;
@property (strong, nonatomic)UIButton *btnForgetpassword;
@end

@implementation LoginVC
- (void)setData
{
    self.isSupportSingleTapHideKeyboard =YES;
    self.title =@"登录";
}
- (void)viewDidLoad {
    [self setData];
    [super viewDidLoad];
    [self setSubviews];
    
    // Do any additional setup after loading the view.
}
- (void)setSubviews {
    self.viewHeadBJ =[[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 120.0)];
    [self.view addSubview:self.viewHeadBJ];

    
    UIImageView *imageText =[[UIImageView alloc] initWithFrame:CGRectMake((DeviceWidth-60.0)/2, 30.0, 60.0, 60.0)];
//    imageText.image =[UIImage imageNamed:@"Appicon"];
    imageText.layer.cornerRadius =3.0f;
    imageText.layer.masksToBounds =YES;
    [self.viewHeadBJ addSubview:imageText];
    
    self.viewCenter =[[UIView alloc] initWithFrame:CGRectMake(0, 120.0, DeviceWidth, 300.0)];
    [self.view addSubview:self.viewCenter];
    
    self.textAccount =[[UITextField alloc] initWithFrame:CGRectMake(20, 5, DeviceWidth-40, 30.0)];
    self.textAccount.placeholder =@"账号/手机号";
    self.textAccount.borderStyle =UITextBorderStyleNone;
    self.textAccount.delegate =self;
    [self.viewCenter addSubview:self.textAccount];
    
    UIView *viewLine =[[UIView alloc] initWithFrame:CGRectMake(20, 40.0, DeviceWidth-40.0f, 0.3)];
    viewLine.backgroundColor =[UIColor colorWithHexString:kcolorNav];
    [self.viewCenter addSubview:viewLine];
    
    self.textPassword =[[UITextField alloc] initWithFrame:CGRectMake(20, 45, DeviceWidth-40, 30.0)];
    self.textPassword.placeholder =@"密码";
    self.textPassword.borderStyle =UITextBorderStyleNone;
    self.textPassword.delegate =self;
    self.textPassword.secureTextEntry =YES;
    [self.viewCenter addSubview:self.textPassword];
    
    UIView *viewLine2 =[[UIView alloc] initWithFrame:CGRectMake(20, 80.0, DeviceWidth-40.0f, 0.3)];
    viewLine2.backgroundColor =[UIColor colorWithHexString:kcolorNav];
    [self.viewCenter addSubview:viewLine2];

    
    self.btnLogin =[[UIButton alloc] initWithFrame:CGRectMake(10, 120, DeviceWidth-20, 40)];
    [self.btnLogin setBackgroundImage:[UIColor createImageWithColor:[UIColor colorWithHexString:kcolorNav]] forState:UIControlStateNormal];
    [self.btnLogin setBackgroundImage:[UIColor createImageWithColor:[UIColor colorWithHexString:kcolorBtnHighlighted]] forState:UIControlStateHighlighted];
    self.btnLogin.layer.cornerRadius =3.0f;
    self.btnLogin.layer.masksToBounds =YES;
    [self.btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    [self.btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnLogin addTarget:self action:@selector(btnLogin:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewCenter addSubview:self.btnLogin];
    
    self.btnForgetpassword =[[UIButton alloc] initWithFrame:CGRectMake(DeviceWidth-80, 170, 60, 30)];
    [self.btnForgetpassword addTarget:self action:@selector(btnForgetpasswordClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnForgetpassword setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.btnForgetpassword setTitleColor:[UIColor colorWithHexString:kcolorNav] forState:UIControlStateNormal];
    [self.btnForgetpassword setTitleColor:[UIColor colorWithHexString:kcolorBtnHighlighted] forState:UIControlStateHighlighted];
    self.btnForgetpassword.titleLabel.font =themeFont15;
    [self.viewCenter addSubview:self.btnForgetpassword];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)btnRemberAccount:(UIButton *)sender {
    sender.selected =!sender.selected;
}
- (void)btnForgetpasswordClick:(UIButton *)sender {

}
- (void)btnLogin:(id)sender
{
    if ([NSString isEmpty:self.textAccount.text]) {
        [self.view makeToast:@"请输入账号" duration:1.0 position:@"center"];
        return ;
    }
    if ([NSString isEmpty:self.textPassword.text]) {
        [self.view makeToast:@"请输入密码" duration:1.0 position:@"center"];
        return ;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *dicParams =[NSDictionary dictionaryWithObjectsAndKeys:self.textAccount.text,@"userPhone",self.textPassword.text,@"userPassword",nil];
    HZMRequest *request =[[HZMRequest alloc] init];
    request.requsetId =kUserLogin;
    request.requestParamDic =dicParams;
    request.callBackDelegate =self;
    [[HZMAPImanager shareMAPImanager] postWithWSRequest:request];
}
#pragma mark--textFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.viewCenter.transform =CGAffineTransformMakeTranslation(0, 0);
    self.viewHeadBJ.transform =CGAffineTransformMakeTranslation(0, 0);
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.viewCenter.transform =CGAffineTransformMakeTranslation(0, -100.0);
    self.viewHeadBJ.transform =CGAffineTransformMakeTranslation(0, -100.0);
}
#pragma mark--HZMAPIManagerDelegate
- (void)transactionFinished:(HZMResponse *)response
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if ([response.requestId isEqualToString:kUserLogin])
    {
        Person *_person =[Person sharePerson];
        [_person initDataWithDic:response.responseData];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kLogined];
        [[NSUserDefaults standardUserDefaults] setObject:response.responseData forKey:kUserDic];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.navigationController.view makeToast:@"已登录" duration:1.0 position:@"center"];
        [self.navigationController popViewControllerAnimated:YES];
      
    }
}
- (void)transactionFailed:(HZMResponse *)response
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [self.view makeToast:response.responseMsg duration:1.0 position:@"center"];
}
@end
